package com.slf.engine.services.helper;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import com.slf.engine.bo.Order;
import com.slf.engine.bo.WaitBean;
import com.slf.engine.common.SysContants;
import com.slf.engine.common.SysContext;
import com.slf.engine.utils.DateUtils;
import com.slf.engine.utils.Utils;
import com.zhkj.wsaction.type.TPacket;


public class ServiceHelper {

	private static final Logger log = Logger.getLogger(ServiceHelper.class);
	/**
	 * 补左空工具方法
	 * @param s 处理字符串
	 * @param len 字段规定长度
	 * @return 返回处理后的字符串
	 */
	public static String addLeftNull(String s, int len)
	{
		if(s.length() == len)
		{
			return s;
		}
		int i = len - s.length();
		if(i >= 0)
		{
			for(int j = 0; j < i; j++)
			{
				s+=" ";
			}
		}
		return s;
	}
	
	/**
	 * 补右空工具方法
	 * @param s 处理字符串
	 * @param len 字段规定长度
	 * @return 返回处理后的字符串
	 */
	public static String addRightNull(String s, int len)
	{
		if(s.length() == len)
		{
			return s;
		}
		int i = len - s.length();
		if(i >= 0)
		{
			for(int j = 0; j < i; j++)
			{
				s = s + " ";
			}
		}
		return s;
	}
	
	/**
	 * 补左0工具方法
	 * @param s 处理字符串
	 * @param len 字段规定长度
	 * @return 返回处理后的字符串
	 */
	public static String addLeftZero(String s, int len)
	{	
		if(s.length() == len)
		{
			return s;
		}
		int i = len - s.length();
		if(i >= 0)
		{
			for(int j = 0; j < i; j++)
			{
				s = "0" + s;
			}
		}
		return s;
	}
	
	
	/**
	 * 生成交易头信息
	 * @return
	 */
	public static StringBuffer makeMsg(String body)
	{
		log.log(Priority.INFO, "msgbody：" + body);
		int msglen = body.length() + 8;
		StringBuffer msg = new StringBuffer();
		byte[] len= new byte[4];
		int byteNum = (body.length() -Integer.numberOfLeadingZeros (msglen < 0 ? ~msglen : msglen))/ 8;
		for (int n = 0; n < byteNum; n++)
		len[3 - n] = (byte) (msglen>>> (n * 8));
		msg.append(new String(len));
		msg.append("0101");
		msg.append(body);
		return msg;
	}
	
	/**
	 * 生成登录方法体
	 * @return
	 */
	public static StringBuffer makeLoginBody()
	{
		StringBuffer body = new StringBuffer();
		body.append("0100");
		body.append(UUID.randomUUID().toString().substring(0, 20));
		body.append(addLeftZero(SysContants.TJFX_LOGINNAME,8));
		body.append(addLeftZero(SysContants.TJFX_PASSWORD,8));
		return body;
	}
	/**
	 * 生成注销方法体
	 * @return
	 */
	public static StringBuffer makeLogoutBody()
	{
		StringBuffer body = new StringBuffer();
		body.append("0110");
		body.append(UUID.randomUUID().toString().substring(0, 20));
		body.append(addLeftZero(SysContants.TJFX_LOGINNAME,8));
		return body;
	}
	
	/**
	 * 生成交易方法体
	 * @return
	 */
	public static StringBuffer makeBusBody(Order order)
	{
		StringBuffer body = new StringBuffer();
		body.append("0200");
		//body.append(UUID.randomUUID().toString().substring(0, 20));
		body.append(addRightNull(order.getOrderId(), 20));
		body.append(addRightNull(order.getAccount(),20));
		body.append(addLeftZero((int)Double.parseDouble(order.getMoney())*100+"", 6));
		body.append(order.getCzrq());
		body.append(order.getCzsj());
		return body;
	}
	
	public static StringBuffer makeQueryBody(Order order)
	{
		StringBuffer body = new StringBuffer();
		body.append("0310");
		body.append(UUID.randomUUID().toString().substring(0, 20));
		body.append(addRightNull(order.getOrderId(),20));
		body.append(order.getCzrq());
		body.append(order.getCzsj());
		return body;
	}
	
	/**
	 * 放入查询队列
	 * @param order
	 * @return
	 */
	public static boolean putToCache(Order order)
	{
		log.log(Priority.INFO, "进入查询等待orderId:" + order.getOrderId() + " account:" + order.getAccount() + " money:" + order.getMoney() + " clbz:" + order.getClbz());
		WaitBean waitBean = new WaitBean();
		synchronized (SysContext.QUERY_MAP) {
			if(!SysContext.QUERY_MAP.containsKey(order.getOrderId()))
			{
				waitBean.setOrder(order);
				waitBean.setIsStart(true);
				waitBean.setTimeout(System.currentTimeMillis());
				SysContext.QUERY_MAP.put(order.getOrderId(), waitBean);
				waitBean.start();
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 放入通知队列
	 */
	public static boolean putToNotice(Order order)
	{
		int i = order.getTzcs();
		order.setTzcs(i++);
		order.setNexttzsj(System.currentTimeMillis() + (long)Math.pow(order.getTzcs(),2));
		SysContext.NOTICE_LIST.add(order);
		return false;
	}
	
	public static Order putPacketToOrder(TPacket packet)
	{
		Order order = new Order();
		order.setOrderId(packet.readFromVarDataAsStr("CZID"));
		order.setAccount(packet.readFromVarDataAsStr("BCZSJH"));
		order.setMoney(Double.parseDouble(packet.readFromVarDataAsStr("CZJE"))/100+"");
		order.setStarttime(DateUtils.strDate("yyyy-MM-dd HH:mm:ss"));
		return order;
	}
	
	public static List<String> splitRecvMsg(String msg)
	{
		List<String> list = new ArrayList<String>();
		
		byte b[] = msg.substring(0, 4).getBytes();
		if(b.length < 4)
		{
			return list;
		}
		int len = b[3];
		if(len == 0)
		{
			return list;
		}
		while(msg.length() >= len)
		{
			if(len == 0)
			{
				return list;
			}
			list.add(msg.substring(0, len));
			if(msg.length() > len)
			{
				msg = msg.substring(len, msg.length());
				b = msg.substring(0, 4).getBytes();
				len = b[3];
			}else{
				return list;
			}
		}
		return list;
	}
}
